<!-- +----------------------------------------------------------------------
| 麦沃德科技赋能开发者，助力中小企业发展 
+----------------------------------------------------------------------
| Copyright (c) 2017～2024  www.wdadmin.cn    All rights reserved.
+----------------------------------------------------------------------
| 沃德代驾系统并不是自由软件，不加密，并不代表开源，未经许可不可自由转售和商用
+----------------------------------------------------------------------
| Author: MY WORLD Team <bd@maiwd.cn>   www.wdadmin.cn
+----------------------------------------------------------------------
| 实名认证 开发者: 麦沃德科技-半夏
+---------------------------------------------------------------------- -->

<template>
	<view class="container" :style="{'--theme-color': themeColor, paddingBottom: isReadOnly ? '32rpx' : '224rpx'}" v-if="loadEnd">
		<!-- 顶部标题 -->
		<view class="container-header">
			<view class="header-title">实名认证</view>
			<view class="header-subtitle">为了保障顺利用车及用车安全，邀请您进行实名认证</view>
		</view>
		<!-- 表单区 -->
		<view class="container-form">
			<view class="form-item flex">
				<view class="item-input flex-item" v-if="isReadOnly">{{formData.name}}</view>
				<input class="item-input flex-item" v-model="formData.name" type="text" placeholder="请输入姓名" placeholder-class="item-placeholder" v-else />
			</view>
			<view class="form-item flex">
				<view class="item-input flex-item" v-if="isReadOnly">{{formData.id_card}}</view>
				<input class="item-input flex-item" v-model="formData.id_card" type="idcard" placeholder="请输入身份证号" placeholder-class="item-placeholder" v-else />
			</view>
		</view>
		<!-- 底部区域 -->
		<view class="container-footer" v-if="!isReadOnly">
			<view class="footer-read flex align-items-center" @click="isRead = !isRead">
				<view class="read-radio" :class="{active: isRead}">
					<image src="/static/tick.png" mode="aspectFit" v-if="isRead"></image>
				</view>
				<view class="read-label">请仔细阅读并同意<text class="text" @click.stop="toAgreement()">《实名认证协议》</text></view>
			</view>
			<view class="footer-btn">
				<view class="btn-box" @click="handleSubmit()">提交认证</view>
			</view>
			<view class="safe-padding"></view>
		</view>
	</view>
</template>

<script>
	import { mapState } from "vuex"
	export default {
		data() {
			return {
				// 加载完成
				loadEnd: false,
				// 表单内容
				formData: {},
				// 表单是否只读
				isReadOnly: false,
				// 是否已读
				isRead: false,
				// 延时器
				timeout: null,
			}
		},
		computed: {
			...mapState({
				themeColor: state => state.app.themeColor,
			})
		},
		onLoad(option) {
			if (option.status == 1) {
				this.isReadOnly = true
				uni.showLoading({
					title: "加载中"
				})
				this.getAuthenticationInfo(() => {
					this.loadEnd = true
					uni.hideLoading()
				})
			} else {
				this.loadEnd = true
			}
		},
		onUnload() {
			clearTimeout(this.timeout)
		},
		methods: {
			// 获取认证信息
			getAuthenticationInfo(fn) {
				this.$util.request("passenger.authentication.details").then(res => {
					if (fn) fn()
					if (res.code == 1) {
						this.formData = res.data
					} else {
						uni.showToast({
							title: res.msg,
							icon: 'none'
						})
					}
				}).catch(error => {
					if (fn) fn()
					console.error('获取认证信息 ', error)
				})
			},
			// 提交认证
			handleSubmit() {
				if (!this.isRead) {
					uni.showToast({
						icon: "none",
						title: "请阅读并同意代驾协议",
					})
					return
				}
				if (!this.formData.name) {
					uni.showToast({
						icon: "none",
						title: "请输入姓名",
					})
					return
				}
				if (!this.formData.id_card) {
					uni.showToast({
						icon: "none",
						title: "请输入身份证号",
					})
					return
				}
				if (!this.$util.validation("IDcard", this.formData.id_card)) {
					uni.showToast({
						icon: "none",
						title: "请输入正确的身份证号"
					})
					return
				}
				uni.showLoading({
					title: "加载中",
					mask: true,
				})
				this.$util.request("passenger.authentication.submit", this.formData).then(res => {
					uni.hideLoading()
					if (res.code == 1) {
						uni.showToast({
							title: "认证成功",
							icon: "success",
							mask: true
						})
						this.timeout = setTimeout(() => {
							uni.navigateBack()
						}, 1000);
					} else {
						uni.showToast({
							title: res.msg,
							icon: 'none'
						})
					}
				}).catch(error => {
					uni.hideLoading()
					console.error('提交认证 ', error)
				})
			},
			// 跳转协议
			toAgreement() {
				this.$util.toPage({
					mode: 1,
					path: "/pages/passenger/mine/settings/agreement?type=5"
				})
			},
		}
	}
</script>

<style lang="scss">
	.container {
		padding: 32rpx 32rpx 224rpx;

		.container-header {
			.header-title {
				color: #242629;
				font-size: 32rpx;
				font-weight: 600;
				line-height: 44rpx;
			}

			.header-subtitle {
				margin-top: 8rpx;
				color: #8D929C;
				font-size: 24rpx;
				line-height: 34rpx;
			}
		}

		.container-form {
			.form-item {
				margin-top: 32rpx;

				.item-input {
					width: 100%;
					color: #242629;
					font-size: 32rpx;
					font-weight: 600;
					height: 128rpx;
					line-height: 128rpx;
					border-radius: 32rpx;
					background: #FFF;
					padding: 0 48rpx;
					box-sizing: border-box;
				}

				.item-placeholder {
					color: #8D929C;
				}
			}
		}

		.container-footer {
			position: fixed;
			left: 0;
			right: 0;
			bottom: 0;
			z-index: 96;
			background: #ffffff;
			border-top: 1rpx solid #F6F7FB;

			.footer-read {
				padding: 20rpx 32rpx;

				.read-radio {
					width: 40rpx;
					height: 40rpx;
					background: #E1E1E1;
					border-radius: 50%;
					overflow: hidden;

					&.active {
						background: var(--theme-color);
					}
				}

				.read-label {
					margin-left: 16rpx;
					color: #8D929C;
					font-size: 28rpx;
					font-weight: 600;
					line-height: 40rpx;

					.text {
						color: var(--theme-color);
					}
				}
			}

			.footer-btn {
				padding: 16rpx 24rpx;

				.btn-box {
					color: #ffffff;
					font-size: 28rpx;
					line-height: 40rpx;
					padding: 20rpx 24rpx;
					border-radius: 40rpx;
					background: var(--theme-color);
					text-align: center;
				}
			}
		}
	}
</style>